<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <button id="choose">切换</button> <br>
    <img src="images/ldh.jpg" alt="" title="刘德华">
    <script>
        var choose = document.querySelector('#choose');
        var img = document.querySelector('img');
        var flag = true;
        // var num = 0;
        choose.onclick = function () {
            num++
            if (flag = !flag) {
                img.src = 'images/zxy.jpg';
                img.title = '张学友思密达';
                // flag = false;
            } else {
                img.src = 'images/ldh.jpg';
                img.title = '刘德华';
                // flag = true;
            }
            //每一次点击的时候falg的值都要取反
            // flag = !flag;
        }
    </script>
</body>

</html>